﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for TAAgeDB
/// </summary>
/// 
namespace CE.Provider
{
    public class TAAgeDB
    {
        public TAAgeDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(TAAge TAAge)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderTAAgeInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();
            #region Parameter

            _sPara.Add("@TAAgeId", TAAge.TAAgeId);
            _sPara.Add("@ProviderCompanyId", TAAge.ProviderCompanyId);
            _sPara.Add("@TAInsID", TAAge.TAInsId);
            _sPara.Add("@AdultAgeFrom", TAAge.AdultAgeFrom);
            _sPara.Add("@AdultAgeTo", TAAge.AdultAgeTo);
            _sPara.Add("@ChildAgeFrom", TAAge.ChildAgeFrom);
            _sPara.Add("@ChildAgeTo", TAAge.ChildAgeTo);
            _sPara.Add("@PlanType", TAAge.PlanType);
            _sPara.Add("@CoverageLevel", TAAge.CoverageLevel);
            _sPara.Add("@RefCEUserId", TAAge.RefCEUserId); 


            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Delete(Int64 TAAgeId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderTAAgeDeleteSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@TAAgeID", TAAgeId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result > 0;
        }


        public TAAge GetAgeEligibilityDTItem(Int64 TAInsId, string PlanType)
        {
            TAAge _TAAge = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderTAAgeSelectSingleItem]";

            SortedList _sPara = new SortedList();
            _sPara.Add("@TAInsID", TAInsId);
            _sPara.Add("@PlanType", PlanType);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _sPara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _TAAge = new TAAge();
                    _TAAge = FillDataRecord(_dt, 0);
                }
            }
            return _TAAge;
        }

        public TAAge GetAgeEligibilityDTItem(Int64 ProviderCompanyId, string PlanType, string CoverageLevel)
        {
            TAAge _TAAge = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderTAAgeSelectSingleItemSelected]";

            SortedList _sPara = new SortedList();
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            _sPara.Add("@PlanType", PlanType);
            _sPara.Add("@CoverageLevel", CoverageLevel);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _sPara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _TAAge = new TAAge();
                    _TAAge = FillDataRecord(_dt, 0);
                }
            }
            return _TAAge;
        }

        private TAAge FillDataRecord(DataTable myDataTable, int rowId)
        {
            TAAge myTAAge = new TAAge();
            myTAAge.TAAgeId = Convert.ToInt64(myDataTable.Rows[rowId]["TAAgeID"].ToString());
            myTAAge.TAInsId = Convert.ToInt64(myDataTable.Rows[rowId]["TAInsID"].ToString());
            myTAAge.AdultAgeFrom = Convert.ToInt32(myDataTable.Rows[rowId]["AdultAgeFrom"].ToString());
            myTAAge.AdultAgeTo = Convert.ToInt32(myDataTable.Rows[rowId]["AdultAgeTo"].ToString());
            myTAAge.ChildAgeFrom = Convert.ToInt32(myDataTable.Rows[rowId]["ChildAgeFrom"].ToString());
            myTAAge.ChildAgeTo = Convert.ToInt32(myDataTable.Rows[rowId]["ChildAgeTo"].ToString());
            myTAAge.PlanType = myDataTable.Rows[rowId]["PlanType"].ToString();
            //myTAAge.PlanName = myDataTable.Rows[rowId]["PlanName"].ToString();
            myTAAge.CoverageLevel = myDataTable.Rows[rowId]["CoverageLevel"].ToString();

            return myTAAge;
        }
    }
}
